package wujinye.com.dao;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ArrayHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import wujinye.com.dao.base.BaseDao;
import wujinye.com.model.ConJoin;
import wujinye.com.utils.JDBCUtil;

import java.sql.SQLException;

public class ConJoinDao extends BaseDao<ConJoin> {
    /**
     * 统计需要参加会议的总人数
     * @param mId
     * @return
     */
    public long getTotalPerson(Integer mId) {
        QueryRunner qr = new QueryRunner(JDBCUtil.getDataSource());
        String sql = "select count(*) from con_join where c_id = ?;";
        long total = 0L;
        try{
            total = qr.query(sql,new ScalarHandler<Long>(),mId);
        }catch (SQLException e){
            e.printStackTrace();
        }
        return total;
    }

    /**
     * 统计未参加会议的人数
     * @param mId
     * @return
     */
    public long getTotalAbsence(Integer mId) {
        QueryRunner qr = new QueryRunner(JDBCUtil.getDataSource());
        String sql = "select count(*) from con_join where c_id = ? and status = 0;";
        long total = 0L;
        try{
            total = qr.query(sql,new ScalarHandler<Long>(),mId);
        }catch (SQLException e){
            e.printStackTrace();
        }
        return total;
    }

    /**
     * 根据用户ID和会议ID，查找用户和会议之间的关系
     * null：用户与该会议无关
     * 0：未出席
     * 1：已参加
     *
     * @param uId
     * @return
     */
    public Object[] isCome(Integer uId,Integer mId) {
        QueryRunner qr = new QueryRunner(JDBCUtil.getDataSource());
        String sql = "select status from con_join where u_id = ? and c_id = ?;";
        Object[] obj = null;
        try{
            obj = qr.query(sql,new ArrayHandler(),uId,mId);
        }catch (SQLException e){
            e.printStackTrace();
        }
        return obj;
    }

    /**
     * 根据会议ID和用户ID，设置该用户确认参加会议
     * @param mId
     */
    public void joinMeeting(Integer mId,Integer uId) {
        String sql = "update con_join set status = 1 where c_id = ? and u_id = ?;";
        Object[] obj = {mId,uId};
        adu(sql,obj);
    }

    /**
     * 添加会议后，添加并设置需要参加会议人员的信息（批量添加）
     * @param params
     */
    public void setStatus(Object[][] params) {
        QueryRunner qr = new QueryRunner(JDBCUtil.getDataSource());
        String sql = "insert into con_join values(?,?,?,?)";
        try {
            qr.batch(sql,params);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
